
//
//  AddAddressViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/27.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "AddAddressViewController.h"
#import "SelectLocationView.h"
#import "IWTextView.h"
#import "WeChatLoginViewController.h"
@interface AddAddressViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * proVinceArray;//地址列表数据
@property (nonatomic, strong)SelectLocationView * myListView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITextField * nameTextField;//姓名
@property (nonatomic, strong)UITextField * phoneNumTextField;//电话号码
@property (nonatomic, strong)UITextField * addressTextField;//地址
@property (nonatomic, strong)IWTextView * detailAddressTextView;//详细地址
@property (nonatomic, strong)NSString * provinceString;//省
@property (nonatomic, strong)NSString * cityString;//市
@property (nonatomic, strong)NSString * areaString;//区
@property (nonatomic, strong)NSString * phoneString;
@property (nonatomic, strong)NSString * tokenString;
- (void)ifLogin;
@end
@implementation AddAddressViewController(networking)
//省地址
- (void)getProvinceData{
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"usiosApi/addressChoice" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [self hiddenProgressHUD];
            __weak typeof (self)weakSelf = self;
            NSArray *itemArray = [[responseObject[@"list"][@"$addressone"] reverseObjectEnumerator] allObjects];
            for (NSDictionary *dic in itemArray){
                [weakSelf.proVinceArray addObject:dic];
            }
            [self.myListView getLocationListArray:weakSelf.proVinceArray];
            [self.myListView showView];
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"400"]) {
            [self ifLogin];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
//提交新增地址
- (void)submitNewAddressData{
    NSDictionary * dic = @{
                           @"phone":self.phoneString,
                           @"token":self.tokenString,
                           @"status":@"0",
                           @"receivename":self.nameTextField.text,
                           @"receivephone":self.phoneNumTextField.text,
                           @"receiveprovince":self.provinceString,
                           @"receivecity":self.cityString,
                           @"receivearea":self.areaString,
                           @"receiveaddress":self.detailAddressTextView.text,
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"usiosApi/addressDo" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"400"]) {
            [self ifLogin];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加地址";
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    if ([[UserModel sharedInstanced].token isEqualToString:@""] || [UserModel sharedInstanced].token == nil || [[UserModel sharedInstanced].token length] == 0 || [[UserModel sharedInstanced].mobile isEqualToString:@""] || [UserModel sharedInstanced].mobile == nil || [[UserModel sharedInstanced].mobile length] == 0) {
        self.tokenString = @"";
        self.phoneString = @"";
    }else{
        self.tokenString = [UserModel sharedInstanced].token;
        self.phoneString = [UserModel sharedInstanced].mobile;
    }
    //地址列表
    self.myListView = [[SelectLocationView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
    __weak typeof (self)weakSelf = self;
    self.myListView.getDetailAddressBlock = ^(NSString * provinceString, NSString * cityString, NSString * areaString) {
        weakSelf.provinceString = provinceString;
        weakSelf.cityString = cityString;
        weakSelf.areaString = areaString;
        weakSelf.addressTextField.text = [NSString stringWithFormat:@"%@%@%@", provinceString, cityString, areaString];
    };
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (NSMutableArray *)proVinceArray{
    if (!_proVinceArray) {
        _proVinceArray = @[].mutableCopy;
    }return _proVinceArray;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 355/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(mainColor);
        [self setUpHeadViewViews];
    }return _headerView;
}
- (void)setUpHeadViewViews{
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(10/WIDTH_6_SCALE, 10/WIDTH_6_SCALE, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 262/WIDTH_6_SCALE)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [_headerView addSubview:whiteView];
    for (int i = 0; i<3; i++) {
        UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(11/WIDTH_6_SCALE, (58 * i)/WIDTH_6_SCALE, whiteView.width - 11/WIDTH_6_SCALE, 57/WIDTH_6_SCALE)];
        textField.font = DEF_FontSize_14;
        textField.tag = 70000 + i;
        textField.backgroundColor = [UIColor clearColor];
        [whiteView addSubview:textField];
        UIView * borderView = [[UIView alloc] initWithFrame:CGRectMake(textField.left, textField.bottom, textField.width, 1/WIDTH_6_SCALE)];
        borderView.backgroundColor = getColor(mainColor);
        [whiteView addSubview:borderView];
    }
    self.nameTextField = (UITextField *)[whiteView viewWithTag:70000];
    self.nameTextField.placeholder = @"收货人姓名";
    self.phoneNumTextField = (UITextField *)[whiteView viewWithTag:70001];
    self.phoneNumTextField.placeholder = @"收货人手机号码";
    self.addressTextField = (UITextField *)[whiteView viewWithTag:70002];
    self.addressTextField.placeholder = @"收货地址";
    self.addressTextField.userInteractionEnabled = NO;
    //收货地址点击事件
    UIButton * addressBtn = [[UIButton alloc] initWithFrame:self.addressTextField.frame];
    [addressBtn addTarget:self action:@selector(addressBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    addressBtn.backgroundColor = [UIColor clearColor];
    [whiteView addSubview:addressBtn];
    //详细地址
    self.detailAddressTextView = [[IWTextView alloc] initWithFrame:CGRectMake(self.nameTextField.left - 5/WIDTH_6_SCALE, self.addressTextField.bottom + 3/WIDTH_6_SCALE, self.addressTextField.width, 90/WIDTH_6_SCALE - 5/WIDTH_6_SCALE)];
    self.detailAddressTextView.placeholder = @"街道、小区门牌等详细地址";
    self.detailAddressTextView.font = DEF_FontSize_14;
    [whiteView addSubview:self.detailAddressTextView];
    //保存按钮
    UIButton * saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, whiteView.bottom + 38/WIDTH_6_SCALE, SCREEN_WIDTH - 30/WIDTH_6_SCALE, 44/WIDTH_6_SCALE)];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:getColor(@"898989") forState:UIControlStateNormal];
    saveBtn.titleLabel.font = DEF_FontSize_14;
    [saveBtn setBackgroundColor:[UIColor whiteColor]];
    [_headerView addSubview:saveBtn];
}
#pragma mark - action
- (void)ifLogin{
    WeChatLoginViewController * vc = [[WeChatLoginViewController alloc] init];
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.backHomePageBlock = ^{
        [vc dismissViewControllerAnimated:YES completion:nil];
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    [self presentViewController:navi animated:YES completion:nil];
}
- (void)addressBtnClick:(UIButton *)sender{
    [self.view endEditing:YES];
    [self getProvinceData];
    
}
- (void)saveBtnClick:(UIButton *)sender{
    if ([self.nameTextField.text isEqualToString:@""] || [self.nameTextField.text length] == 0 || self.nameTextField.text == nil) {
        ShowMessage(@"请填写收货人姓名");
        return;
    }
    if ([self.phoneNumTextField.text isEqualToString:@""] || [self.phoneNumTextField.text length] == 0 || self.phoneNumTextField.text == nil) {
        ShowMessage(@"请填写收货人电话号码");
        return;
    }
    if ([self.addressTextField.text isEqualToString:@""] || [self.addressTextField.text length] == 0 || self.addressTextField.text == nil) {
        ShowMessage(@"请填写收货人地址");
        return;
    }
    if ([self.detailAddressTextView.text isEqualToString:@""] || [self.detailAddressTextView.text length] == 0 || self.detailAddressTextView.text == nil) {
        ShowMessage(@"请填写收货人详细地址");
        return;
    }
    if ([self.areaString isEqualToString:@""] || self.areaString == nil || [self.areaString length] == 0) {
        ShowMessage(@"请填写收货人地址");
        return;
    }
    [self submitNewAddressData];
}
@end
